/*
* @Author: suddev
* @Date:   2017-08-02 10:39:14
* @Last Modified by:   suddev
* @Last Modified time: 2017-08-02 11:09:15
*/

'use strict';

//表单里的错误提示
var formError = {
    show    : function(errorMsg){
        $('.error-item').show().find('.error-message').text(errorMsg);
    },
    hide    : function(){
        $('.error-item').hide().find('.error-message').text('');
    }
};

var page = {
	init		: function(){
        this.bindEvent();
	},
	bindEvent	: function(){
		var _this = this;
		$('#submit').click(function(){
			_this.submit();
		});
	},
	// 提交表单
    submit : function () {
        var formData = {
            username : $.trim($('#username').val()),
            password : $.trim($('#password').val())
        },
        //表单验证结果
        validateResult = this.formValidate(formData);
        // 验证成功
        if (validateResult.status) {
            //提交
            _user.login(formData,function(res){
                window.location.href = './index.html';
            },function(errorMsg){
                formError.show(errorMsg);
            });
        }
        //验证失败
        else{
            // 错误提示
            formError.show(validateResult.msg);
        }
    },
    formValidate : function(formData){
        var result = {
            status  : false,
            msg     : ''
        };
        if (!_tools.validate(formData.username,'require')) {
            result.msg = '用户名不能为空';
            return result;
        }
        if (!_tools.validate(formData.password,'require')) {
            result.msg = '密码不能为空';
            return result;
        }
        //通过验证，返回正确提示
        result.status = true;
        result.msg    = '验证通过';
        return result;
    }
}

$(function () {
   page.init(); 
});